package com.webserve.usercontrol;

import com.webserve.http.HttpServletRequest;
import com.webserve.http.HttpServletResponse;
import com.webserve.user.User;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class UserController {
    private static File dirFile ;

    static {
        dirFile = new File("./user");
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
    }



    public void reg(HttpServletRequest request , HttpServletResponse response)throws IOException {
        String name = request.getParameters().get("name");
        String password = request.getParameters().get("password");
        String qq = request.getParameters().get("qq");
        String gender = request.getParameters().get("gender");
        System.out.println("name:"+name+","+"password:"+password+","+"qq:"+qq+","+"gender:"+gender);

        if (name==null||name.equals("")||
                password==null||password.equals("")||
                gender==null||gender.equals("")||
                qq==null||qq.equals("")||
                !qq.matches("[0-9]+")
                ){
            response.sendRedirect("/static/reg_info_filed.html");
            return;
        }

        User user = new User(name,password,qq,gender);
        File file = new File(dirFile,name+".obj");
        if (file.exists()){
            response.sendRedirect("reg_filed.html");
            return;
        }
        response.sendRedirect("reg_success.html");
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(user);

    }

}
